﻿window.Reload = function () {
    window.location.href = window.location.href.split("#")[0];
};

function GetUrl(action, controller) {
    return $("#root-url").val() + controller + "/" + action;
}

String.prototype.IsDate = function () {
    return this.match(/^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?[2|02]-29))$/);
}

function TransferDate(bdate) {
    if (!bdate || bdate == "")
        return ""
    var time = parseInt(bdate.split('(')[1].split(')')[0]);
    var fViolatedDate = new Date(time);
    return fViolatedDate.getFullYear() + "-" + (fViolatedDate.getMonth() + 1) + "-" + fViolatedDate.getDate();
}


function StringBuilder() {
    this._strings_ = new Array();
}

StringBuilder.prototype.append = function (str) {
    this._strings_.push(str);
};

StringBuilder.prototype.toString = function () {
    return this._strings_.join("");
};

// Cookie
function setCookie(name, value) {
    var Days = 1;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);

    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/;";
}

function getCookie(name, clear) {
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (clear)
        DelCookie(name);
    if (arr != null) return unescape(decodeURIComponent(arr[2])); return null;
}

function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 10000);
    var cval = getCookie(name);
    document.cookie = name + "=" + encodeURIComponent(cval).toLowerCase() + "; path=/; expires=" + exp.toGMTString();
}

function setUrlParam(name, value, url) {
    if (url == undefined || url == null) {
        url = window.location.href;
    }

    var array = url.split('?');
    var url = array[0];
    if (array.length <= 1) {
        return url + "?" + name + "=" + escape(value);
    }

    var added = false;
    var parameters = array[1].split("&");
    var newParams = "";
    for (var i in parameters) {
        var tmp = parameters[i].split("=");
        var param_name = tmp[0];
        var param_value = tmp[1];
        if (param_name.toLowerCase() == name.toLowerCase()) {
            param_value = value;
            added = true;
        }
        if (newParams.length > 0) {
            newParams += "&";
        }
        newParams += param_name + "=" + escape(param_value);
    }

    if (!added) {
        if (newParams.length > 0) {
            newParams += "&";
        }
        newParams += name + "=" + escape(value);
    }

    return url + "?" + newParams;
}

function getUrlParam(name, url) {
    if (url == undefined || url == null) {
        url = window.location.href;
    }

    var array = url.split('?');
    if (array.length <= 1) return null;

    var parameters = array[1].split("&");
    for (var i in parameters) {
        var tmp = parameters[i].split("=");
        var param_name = tmp[0];
        var param_value = tmp[1];
        if (param_name.toLowerCase() == name.toLowerCase()) {
            return param_value;
        }
    }

    return null;
}